﻿using System;
using Microsoft.Popfly.GameCreator.GameEngine.Events;

namespace Microsoft.Popfly.GameCreator.GameEngine.Behaviors
{
    public class StateChangeBehavior : IBehavior
    {
        private  String m_stateName;
        private Actor m_actor;

        public StateChangeBehavior(BehaviorOwner owner, Data.BehaviorInfo behaviorInfo)
        {
            if (behaviorInfo.GetType() != typeof(Data.StateChangeBehaviorInfo))
            {
                throw new ArgumentException(
                    Utils.ErrorMessagePrefix +
                    "StateChangeBehaviors require a behaviorInfo of type StateChangeBehaviorInfo.  behaviorInfo was of type '" +
                    behaviorInfo.GetType().ToString() +
                    "'.");
            }
            if (!owner.IsActor)
            {
                throw new ArgumentException(
                    Utils.ErrorMessagePrefix +
                    "StateChangeBehaviors only apply to actors.  The owner was of type '" +
                    owner.GetType().ToString() +
                    "'.");
            }

            Data.StateChangeBehaviorInfo stateInfo = (Data.StateChangeBehaviorInfo)behaviorInfo;

            m_stateName = stateInfo.StateName;

            m_actor = (Actor)owner;
        }

        public StateChangeBehavior(Actor actor, string stateName)
        {
            m_stateName = stateName;
            m_actor = actor;
        }

        public void Invoke(BehaviorOwner sender, PGCEventArgs args)
        {
            m_actor.ChangeState(m_stateName);
        }
    }
}